<!DOCTYPE html>
<?php
    
    include "DBconf.php";
    include "Redirect.php";
    
    
    $pass="";
    $name="";
    $data_nasc="";
    $email="";
    
    session_start();
    if(!isset($_SESSION['uid']))
    {
        redirect("401.html");
    }
    else
    {
        if(isset($_POST['password'])) $pass = $_POST['password'];
        if(isset($_POST['name'])) $name = $_POST['name'];
        if(isset($_POST['birth'])) $data_nasc = $_POST['birth'];
        if(isset($_POST['email'])) $email = $_POST['email'];
        $connection = mysql_connect($server, $db_user, $db_pass);
        $queryUser = "SELECT * FROM usuario WHERE uid = ".$_SESSION['uid'];
        
        if (mysql_select_db($db_name, $connection)) 
        {
            if ($pass != '' || $name != '' || $data_nasc != '' || $email != '')
            {
                $queryUpt = "UPDATE usuario SET senha = '$pass',
                    nome = '$name', dat_nasc = str_to_date('$data_nasc','%d/%m/%Y'),
                    email = '$email'
                    WHERE uid = ".$_SESSION['uid'];
                
                $resultadoUpt = mysql_query($queryUpt, $connection);
                
                if (!$resultadoUpt)
                {
                    $status['message'] = mysql_error($connection);
                    $status['error'] = true;
                }
                else
                {
                    $status['message'] = 'Usuário alterado.';
                    $status['error'] = false;
                }
            }
            
            $resultado = mysql_query($queryUser, $connection);
            if ($resultado)
            {
                $dados = mysql_fetch_array($resultado);
                //inverte a data para o padrão brasileiro de data
                $data = implode("/", array_reverse(explode("-", $dados['dat_nasc'])));
            }
            else 
            {
                $status['message'] = mysql_error($connection);
                $status['error'] = true;
            }
        }
        else 
        {
            $status['message'] = 'Erro na conexão com o BD.';
            $status['error'] = true;
        }
    }
?>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Kiwitter</title>
        <link type="text/css" rel="stylesheet" href="css/bootstrap.css"/>
        <link type="text/css" rel="stylesheet" href="css/datepicker.css"/>
        <link rel="shortcut icon" href="img/kiwicon.ico">
    </head>
    <body>
        
        <!--###########Menu Bar topo###############-->
        <div class="navbar navbar-fixed-top">
            <div class="navbar-inner">
                <div class="container">
                    <a class="brand" href="index.php">Kiwitter</a>
                    <ul class="nav">
                        <li class="active"><a href="#">Dados de <?php echo $_SESSION['username']?> </a></li>
                        <li class=""><a href="index.php">Inicio</a></li>
                        <?php
                            if (isset($_SESSION['uid'])) {
                                ?>
                                <li class=""><a href="mural.php">Mural</a></li>
                            </ul>
                            <ul class="nav pull-right">
                                <li class="divider-vertical"></li>
                                <li class="dropdown">
                                    <a data-toggle="dropdown" class="dropdown-toggle" href="#">
                                        <?php echo($_SESSION['username']); ?><b class="caret"></b>
                                    </a>
                                    <ul class="dropdown-menu">
                                        <li><a href="#">Meus dados</a></li>
                                        <li class="divider"></li>
                                        <li><a href="SessionControl.php?action=logout">Sair</a></li>
                                    </ul>
                                </li>
                            </ul>
                        <?php } ?>
                </div>
            </div>
        </div>
        
        <!-- #########Alertas no topo, dados abaixo#######-->
        <div class="container">
            <?php
            if (isset($status['error'])) {
                if ($status['error']) {
                    ?>
                    <div class="alert alert-error">
                        <button class="close" data-dismiss="alert">×</button>
                        <strong>Oops!</strong> <?php echo $status['message']; ?>
                    </div>
                <?php } else { ?>
                    <div class="alert alert-success">
                        <button class="close" data-dismiss="alert">×</button>
                        <i class="icon-ok"></i>&nbsp;Os seus dados foram alterados!&nbsp;
                    </div>
                    <?php
                }
            }
            ?>
            <div class="page-header">
                <div class="span8">
                    <?php
                        echo "<h2> Olá, seja bem vindo ".$_SESSION['username']."!</h2>";
                    ?>
                </div>
                <div class="btn-group">
                     <a href="#alterDados" data-toggle="modal" 
                        class="btn btn-inverse"> Alterar Dados
                     </a>
                </div>
            </div>
            <div class="row">
                <div class="span4">
                    <ul class="thumbnails">
                        <li class="span3">
                            <a href="#perfImg" data-toggle="modal" class="thumbnail">
                                <img src="img/kiwi.png" widht="280" height="160">
                            </a>
                        </li>
                    </ul>
                </div>
                <div class="span8">
                    <?php
                        echo "<h3> Sobre você: </h3><br>";
                    ?>
                    <?php
                        echo "<b>Nome:</b> <i style = 'margin-left: 97px'>".$dados['nome']."</i><br>";
                        echo "<b>Email:</b> <i style = 'margin-left: 97px'>".$dados['email']."</i><br>";
                        echo "<b>Data de Nascimento:</b> <i style = 'margin-left: 10px'>".$data."</i><br><br>";
                    ?>
                </div>
            </div>
        </div>
        
        <!-- ########Alterar Dados######-->
        <div class ="modal hide fade" id="alterDados">
            <div class="modal-header">
                <button class="close" data-dismiss="modal">×</button>
                <h3>Alterar Dados</h3>
            </div>
            <div class="modal-body">
                <div id="error-alert"></div>
                <div class="row">
                    <form class="form-horizontal" id="cadastro-form" method="POST" 
                        action="" onsubmit="return valida_form('cadastro-form')">
                        <fieldset>
                            <div class="control-group">
                                <label class="control-label" for="input2"><p>Senha</p></label>
                                <div class="controls">
                                    <input class="input-xlarge" id ="input2" type="password" name="password" value="<?php echo $dados['senha']; ?>" />
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="input3"><p>Nome</p></label>
                                <div class="controls">
                                    <input class="input-xlarge" id="input3" type="text" name="name" value="<?php echo $dados['nome']; ?>" />
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="input4"><p>Aniversário</p></label>
                                <div class="controls">
                                    <input class="input-xlarge" id="input4" type="text" name="birth" value="<?php echo $data; ?>"
                                        data-date-format="dd/mm/yyyy" value="01/01/1991"/>
                                </div>
                            </div>
                            <div class="control-group" id="cad-email">
                                <label class="control-label" for="input5"><p>E-mail</p></label>
                                <div class="controls">
                                    <input class="input-xlarge" id="input5" type="text" name="email"
                                        onkeyup="valida_email('cad-email')"
                                        onchange="valida_email('cad-email')"
                                        value="<?php echo $dados['email']; ?>" />
                                    <span class="help-inline"></span>
                                </div>
                            </div>
                        </fieldset>
                </div>
            </div>
            <div class="modal-footer">
                <a href="#" data-dismiss="modal" class="btn">Cancelar</a>
                <input type="submit" class="btn btn-primary" value="Alterar">
            </div>
            </form>
            </div>
        </div>
        
        <!-- ########Imagem Perfil######-->
        <div class ="modal hide fade" id="perfImg">
            <div class="modal-header">
                <button class="close" data-dismiss="modal">×</button>
                <h3>Imagem do Perfil de <?php echo $dados['nome']; ?> </h3>
            </div>
            <div class="modal-body">
                <center>
                    <img src="img/kiwi.png">
                </center>
            </div>
        </div>
        <script type="text/javascript" src="js/jQuery-1.7.1-min.js"></script>
        <script type="text/javascript" src="js/bootstrap-2.0.3.js"></script>
        <script type="text/javascript" src="js/bootstrap-datepicker.js"></script>
        <script type="text/javascript">
            $('#input4').datepicker();
        </script>
        <script type="text/javascript" src="js/validacao.js"></script>
        <script type="text/javascript" src="js/perfil.js"></script>
    </body>
</html>    